******************************************************************************************
********** THIS .DO FILE PERFORMS THE BALANCE TEST ON CANDIDATE CHARACTERISTICS **********
******************************************************************************************

clear *

* open candidate characteristics data
use "${root}/data/processed/final_candidate_characteristics.dta"

* take logs of wealth (so estimates are interpreted as percentage point differences)
gen left_wealth=ln(left_self_reported_wealth)
gen nonleft_wealth=ln(nonleft_self_reported_wealth)

	
********* Perform (robust bias-corrected) RD estimates and produce results table **********

foreach outcome in left_higher_ed 	 				///
				   nonleft_higher_ed 				///
				   left_incumbent					///
				   nonleft_incumbent				///
				   left_cand_exp_share				///
				   left_com_exp_share			 	///
				   left_wealth						///
				   nonleft_wealth 					{
				   	
	qui areg `outcome' yy*, absorb(mun_code)
	qui predict res_`outcome', res
		

	*** (1a) without controls (whole sample, CCT 2014 MSE-optimal bandwidth)
	rdrobust `outcome' margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all
	
	scalar coeff1_`outcome' = e(tau_bc)
	scalar se1_`outcome'    = e(se_tau_rb)
	scalar pv1_`outcome'    = e(pv_rb)
	scalar n1_`outcome' 	= e(N)
	scalar effn1_`outcome'  = e(N_h_l) + e(N_h_r)	
	
	if "`outcome'"=="left_higher_ed" {
		mat baseline= (coeff1_`outcome', se1_`outcome')
	}

	else if "`outcome'"!="left_higher_ed" & "`outcome'"!="left_wealth" & "`outcome'"!="nonleft_wealth" {
		mat baseline = (baseline \ coeff1_`outcome', se1_`outcome')
	}	
	
	if "`outcome'"=="left_wealth" {
		mat baseline_wealth = (coeff1_`outcome', se1_`outcome')
	}

	if "`outcome'"=="nonleft_wealth" {
		mat baseline_wealth = (baseline_wealth \ coeff1_`outcome', se1_`outcome')
	}	
	
	*** (1b) with FEs (whole sample, CCT 2014 MSE-optimal bandwidth)
	rdrobust res_`outcome' margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all
	
	scalar coeff1b_`outcome' = e(tau_bc)
	scalar se1b_`outcome'    = e(se_tau_rb)
	scalar pv1b_`outcome'    = e(pv_rb)
	scalar n1b_`outcome' 	= e(N)
	scalar effn1b_`outcome'  = e(N_h_l) + e(N_h_r)	
	
	if "`outcome'"=="left_higher_ed" {
		mat fe= (coeff1b_`outcome', se1b_`outcome')
	}

	else if "`outcome'"!="left_higher_ed" & "`outcome'"!="left_wealth" & "`outcome'"!="nonleft_wealth" {
		mat fe = (fe \ coeff1b_`outcome', se1b_`outcome')
	}		
	
	if "`outcome'"=="left_wealth" {
		mat fe_wealth = (coeff1b_`outcome', se1b_`outcome')
	}

	if "`outcome'"=="nonleft_wealth" {
		mat fe_wealth = (fe_wealth \ coeff1b_`outcome', se1b_`outcome')
	}	
	
}

* reshape matrixes in preparation for use in coefplot	
	
foreach subsample in baseline fe {
	
	matrix list `subsample'
	matrix `subsample'_t = `subsample''
	
	if "`subsample'"!="lameduck" & "`subsample'"!="lameduck_fe" {
		matrix colnames `subsample'_t = "Left educ" "Conservative educ" "Left incumbent" "Conservative incumbent" "Left candidate expenditure" "Left committee expenditure" 
		}
		
	else if "`subsample'"=="lameduck" | "`subsample'"=="lameduck_fe" {
		dis as text "lameduck matrix follows"
		matrix colnames `subsample'_t = "Left educ" "Conservative educ" "Left candidate exp." "Left committee exp." 		
	}
	
	matrix list `subsample'_t
	}	

foreach wsample in baseline_wealth fe_wealth {
	
	mat list `wsample'
	mat `wsample'_t = `wsample''
	
	matrix colnames `wsample'_t = "Left candidate wealth" "Conservative candidate wealth"
	
	mat list `wsample'_t
	}	
	
*graph baseline
coefplot (matrix(baseline_t), se(2) color(red) ciopts(color(red) recast(rcap)) msymbol(oh) label("Without FEs")) ///
		(matrix(fe_t), 		se(2) color(blue) ciopts(color(blue) recast(rcap)) msymbol(oh) label("With FEs")), ///
		xline(0) scheme(plotplain) legend(off) xsize(6) ysize(6) name(baseline) xlabel(-0.3(0.1)0.3)	

coefplot (matrix(baseline_wealth_t), se(2) color(red) ciopts(color(red) recast(rcap)) msymbol(oh) label("Without FEs")) ///
		(matrix(fe_wealth_t), 		se(2) color(blue) ciopts(color(blue) recast(rcap)) msymbol(oh) label("With FEs")), ///
		xline(0) scheme(plotplain) legend(rows(1) region(lwidth(none)) position(6)) xsize(6) ysize(4)  fysize(35) name(baseline_wealth) ylabel(1 `" "Left candidate" "wealth" "' 2 `" "Conservative candidate" "wealth" "') xlabel(-0.6(0.1)0.4)
		
graph combine baseline baseline_wealth, rows(1) cols(1) xsize(3) ysize(4)  scheme(plotplain) name(baseline_graph)	
graph export "${root}/results/figures/candidate_characteristics_baseline.pdf", name(baseline_graph) replace

mat fe_all = fe_t, fe_wealth_t

* graph only FE specification (as requested by reviewer)
coefplot matrix(fe_all), 		se(2) color(black) ciopts(color(black) recast(rcap)) msymbol(oh) ///
							xline(0) scheme(plotplain) legend(off) xsize(6) ysize(4) name(rev_baseline) xlabel(-0.3(0.1)0.3)	
graph export "${root}/results/figures/revised_candidate_characteristics_baseline.pdf", name(rev_baseline) replace

